<template>
  <div>
    <a-form ref="formRef" :model="formState" :label-col="{ style: { width: '100px' } }" class="mt-4 form-style-flex">
      <a-form-item name="appDeptCode" label="预约科室" class="w-1/3" v-bind="validateInfos.appDeptCode">
        <!-- <a-select v-model:value="formState.appDeptCode" :options="deptList" placeholder="请选择" allow-clear @select="onSelectDepartment" /> -->
        <SelectSection v-model:value="formState.appDeptCode" :search-params="{ yxzt: '0' }" @change="getDepositPrice" />
      </a-form-item>
      <a-form-item name="wardId" label="预约病区" class="w-1/3" v-bind="validateInfos.wardId">
        <!-- <SelectArea v-model:value="formState.wardId" allow-clear @change="handlerSelectArea" /> -->
        <a-select
          v-model:value="formState.wardId"
          :options="wardList"
          :field-names="{ label: 'wardName', value: 'id' }"
          placeholder="请选择"
          @change="handlerSelectArea"
        />
      </a-form-item>
      <a-form-item name="bedCode" label="目标床位" class="w-1/3">
        <!-- <a-input v-model:value="formState.bedCode" placeholder="请输入" /> -->
        <SelectTable
          v-model:value="formState.bedCode"
          type="床位"
          :placeholder="formState.wardId ? '请选择' : '请选择病区'"
          :disabled="!formState.wardId"
          :search-params="{ bqid: formState.wardId }"
          :row-disabled="(row) => row.status === 2 || row.status === 1"
          @select="onSelectWard"
        />
      </a-form-item>
      <a-form-item name="dutyDoctorName" label="责任医生" class="w-1/3">
        <SelectTable v-model:value="formState.dutyDoctorName" type="医生" placeholder="请选择" @select="onSelect($event, 'dutyDoctor')" />
      </a-form-item>
      <a-form-item name="inpDoctorName" label="住院医生" class="w-1/3">
        <SelectTable v-model:value="formState.inpDoctorName" type="医生" placeholder="请选择" @select="onSelect($event, 'inpDoctor')" />
      </a-form-item>
      <a-form-item name="attendingDoctorName" label="主治医生" class="w-1/3">
        <SelectTable v-model:value="formState.attendingDoctorName" type="医生" placeholder="请选择" @select="onSelect($event, 'attendingDoctor')" />
      </a-form-item>
      <a-form-item name="directorDoctorName" label="主任(副)医生" class="w-1/3">
        <SelectTable v-model:value="formState.directorDoctorName" type="医生" placeholder="请选择" @select="onSelect($event, 'directorDoctor')" />
      </a-form-item>
      <a-form-item name="expectDays" label="预计住院天数" class="w-1/3">
        <a-input v-model:value="formState.expectDays" placeholder="请输入" />
      </a-form-item>
      <a-form-item name="depositAmount" label="押金" class="w-1/3">
        <a-input v-model:value="formState.depositAmount" v-input:int placeholder="请输入" @blur="onBlurPrice" />
      </a-form-item>
      <a-form-item label="入院诊断" class="w-2/3">
        <a-input-group compact>
          <SelectTable v-model:value="formState.outpIco10codeName" type="诊断" class-name="w-3/4" placeholder="请选择" @select="admissionAiagnosis" />
          <a-input v-model:value="formState.outpIco10code" class="w-1/4" placeholder="诊断代码" disabled />
        </a-input-group>
      </a-form-item>
      <a-form-item name="dictItemName" label="入院途径" class="w-1/3">
        <SelectItem v-model:value="formState.dictItemName" type="DT00013" trigger="focus" placeholder="请选择" @select="selectDict($event)" />
      </a-form-item>
      <a-form-item name="readyOperation" label="行何手术" class="w-2/3">
        <a-input v-model:value="formState.readyOperation" placeholder="请输入" />
      </a-form-item>
      <a-form-item name="bw" label="部位" class="w-1/3">
        <a-select v-model:value="formState.bw" placeholder="请选择" mode="multiple" :max-tag-count="3" :options="positionList" allow-clear />
      </a-form-item>
      <a-form-item name="memo" label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="20" placeholder="请输入" />
      </a-form-item>
    </a-form>
    <a-space class="flex justify-end">
      <a-button @click="onPrint">打印</a-button>
      <a-button @click="getData">重置</a-button>
      <a-button type="primary" :disabled="!isCompleted" @click="saveData">保存</a-button>
    </a-space>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post, get } from '@/utils/request'
// import SelectTable from '@/components/SelectTable'
// import SelectArea from '@/components/SelectArea'
import SelectTable from '@/components/SelectTable'
import userStore from '@/store/user'

const { empId } = userStore()
import useStore from '../store'

const store = useStore()
const formRef = ref(null)
const formState = ref({ bw: [], appDeptCode: null, wardId: null })
const deptList = ref([])
const positionList = ref([])
const wardList = ref([])
const depositPrice = ref(0)
const rulesState = ref({
  appDeptCode: [{ required: true, message: '请选择预约科室' }],
  wardId: [{ required: true, message: '请选择预约病区' }]
})
const { isCompleted, validateInfos } = useForm(formState, rulesState)

//科室
const getDept = async () => {
  const { code, data, msg } = await post('/operation/opex/getDeptInfoList', { yxzt: '0' })
  if (code !== 0) return message.error(msg)
  deptList.value = data
}
// 员工信息（拿登录人科室）
const getEmpInfo = async () => {
  const { code, data, msg } = await get('/common/emp/getEmpInfo?empid=' + empId)
  if (code !== 0) return message.error(msg)
  formState.value.appDeptCode = data.ksdm
}

const getPositionList = async () => {
  const { code, data } = await post('/operation/opex/getDictPartList', {
    zjm: '',
    pageNum: 1,
    pageSize: 999
  })
  if (code !== 0) return
  positionList.value = data.map((item) => {
    return {
      value: item.id,
      label: item.partName
    }
  })
}

const handlerSelectArea = () => {
  formState.value.bedCode = null
}

//获取数据
const getData = async () => {
  const { data, code, msg } = await post('/inp/zydj/getInpAppointmentRecord', {
    jzbh: store.currentPatient.jzbh //就诊编号
  })
  if (code !== 0) return message.error(msg)
  if (data) {
    formState.value = data
    formState.value.bw = data.bw ? data.bw : undefined
  } else {
    formRef.value.resetFields()
    formState.value.outpIco10code = ''
    formState.value.wardId = null
    getEmpInfo()
  }
  formState.value.outpIco10codeName = data?.outpIco10codeName || store.currentMedical?.bcLczdZdmc
  formState.value.outpIco10code = data?.outpIco10code || store.currentMedical?.bcLczdZddm
}

const getDepositPrice = async () => {
  const { data, code, msg } = await post('/operation/opex/getInpDeptDepositSetList', {
    mcbh: '',
    ksdm: formState.value.appDeptCode
  })
  if (code !== 0) return message.error(msg)
  if (data) {
    depositPrice.value = data[0].depositLimit
  }
}

const saveData = async () => {
  const { code, msg } = await post('/inp/zydj/doSaveInpAppointmentRecord', {
    ...formState.value,
    brId: store.currentPatient.brId,
    keycode: store.currentPatient.jzbh,
    outpDoctor: empId
  })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}
const onSelect = (select, key) => {
  formState.value[key] = select.empId
  formState.value[key + 'Name'] = select.empName
}
const onSelectWard = (select) => {
  formState.value.bedCode = select.bedCode
}

const admissionAiagnosis = (select) => {
  formState.value.outpIco10codeName = select.zdmc
  formState.value.outpIco10code = select.zddm
}
const selectDict = (val) => {
  formState.value.admissionRoadCode = val
}

const onBlurPrice = () => {
  if (formState.value.appDeptCode && depositPrice.value) {
    if (Number(depositPrice.value) > Number(formState.value.depositAmount)) {
      message.error('不可低于押金下限')
      formState.value.depositAmount = ''
    }
  }
}

// 获取预约病区
const getWardData = async () => {
  const { code, data } = await post('/operation/opex/getWardInfoList', { wardTypeCodes: ['QYLB001'] })
  if (code !== 0) return message.error(code)
  wardList.value = data
}

// 打印
const onPrint = async () => {
  const { code, msg, data } = await post('/inp/Print/hospitalizationNoticesPrint', { jzbh: store.currentPatient.jzbh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}

onMounted(() => {
  getDept()
  getData()
  getPositionList()
  getWardData()
})
</script>
<style lang="less" scoped>
.ant-col {
  padding-right: 0 !important;
}
.ant-row {
  margin-right: 0 !important;
}
</style>
